import NProgress from "nprogress"; // 引入NProgress
import "nprogress/nprogress.css"; // 引入nprogress样式文件
import router from "./index"; // 引入路由文件
import { getToken } from "@/stores/modules/home";
NProgress.configure({
  easing: "ease", // 动画方式
  speed: 500, // 递增进度条的速度
  showSpinner: false, // 是否显示加载ico
  trickleSpeed: 200, // 自动递增间隔
  minimum: 0.3, // 初始化时的最小百分比
});

class Intercept {
  static whiteList = [];
  static setRouterBefore() {
    router.beforeEach((to, from, next) => {
      console.log(to, from, "dd");

      NProgress.start();

      if (getToken()) {
        if (to.path === "/login") {
          next({ path: "/home" });
        } else {
          next();
        }
      } else if (!getToken()) {
        if (to.path == `/login`) {
          next();
        } else {
          next(`/login?redirect=${to.path}`);
        }
      }
    });
  }
  static setRouterAfter() {
    router.afterEach((to, from, next) => {
      NProgress.done();
    });
  }
}
Intercept.setRouterBefore();
Intercept.setRouterAfter();
export default Intercept;
